package deng.jpwh.auction;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import deng.jpwh.EntityManagerUtil;

public class Main {

	public static void main(String[] args) {
		
		EntityManager em = EntityManagerUtil.getEntityManagerFactory().createEntityManager();
		
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		
		Category cat = new Category();
		cat.setName("Bags");
		
		Category subCat1 = new Category();
		subCat1.setName("Handbags");
		subCat1.setParent(cat);
		
		Category subCat2 = new Category();
		subCat2.setName("Backpack");
		subCat2.setParent(cat);
		
		cat.getSubCategories().add(subCat1);
		cat.getSubCategories().add(subCat2);
		
		em.persist(cat);
		em.persist(subCat1);
		em.persist(subCat2);
		
		List<?> ls = em.createQuery("from Category").getResultList();
		for (Object object : ls) {
			Category c = (Category)object;
			System.out.println(c);
		}
		
		tx.commit();
		em.close();
		EntityManagerUtil.shutdown();

	}

}
